package com.captjack.generator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * mybaties-plus代码生成器
 *
 * @author Capt Jack
 * @date 2018/6/14
 */
public class MybatiesPlusGenerator {

    private static final String OUT_PUT_DIR = getOutPutDir();

    /**
     * @return 生成CODE输出目录
     */
    private static String getOutPutDir() {
        String os = System.getProperty("os.name");
        if (os.contains("Mac")) {
            return "/Users/captjack/Desktop";
        } else if (os.contains("Windows")) {
            return "C:\\generator";
        }
        return os;
    }

    /**
     *
     */
    public static void main(String[] args) {
        System.out.println(OUT_PUT_DIR);
        String packageName = "com.captjack.mybatiesplus";
        // user -> UserService, 设置成true: user -> IUserService
        // 需要生成的表名
        String[] tableNames = new String[]{"sys_dept", "sys_dict", "sys_expense", "sys_login_log", "sys_menu", "sys_notice", "sys_operation_log", "sys_relation", "sys_role", "sys_user"};
        // 执行生成
        generateByTables(false, packageName, tableNames);
    }

    private static void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://localhost:3306/guns";
        // 数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("root")
                .setDriverName("com.mysql.jdbc.Driver");
        // 生成策略配置，例：需要继承的基类等配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setRestControllerStyle(true)
                .setCapitalMode(true)
                .setSuperMapperClass("com.captjack.mybatiesplus.mapper.base.CustomizeBaseMapper")
                .setEntityLombokModel(false)
                .setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel)
                // 需要过滤的表名
                .setExclude()
                // 修改替换成需要的，多个表名传数组
                .setInclude(tableNames);
        // 全局配置
        config.setActiveRecord(false)
                .setBaseColumnList(true)
                .setBaseResultMap(true)
                .setEnableCache(true)
                .setAuthor("Capt Jack")
                .setOutputDir(OUT_PUT_DIR)
                .setFileOverride(true);
        if (!serviceNameStartWithI) {
            config.setServiceName("%sService");
        }
        // 自定义模版配置
        TemplateConfig customizeTemplateConfig = new TemplateConfig();
        customizeTemplateConfig.setController("/mybatiesplustemplate/controller.java.vm");
        customizeTemplateConfig.setEntity("/mybatiesplustemplate/entity.java.vm");
        customizeTemplateConfig.setMapper("/mybatiesplustemplate/mapper.java.vm");
        customizeTemplateConfig.setXml("/mybatiesplustemplate/mapper.xml.vm");
        customizeTemplateConfig.setService("/mybatiesplustemplate/service.java.vm");
        customizeTemplateConfig.setServiceImpl("/mybatiesplustemplate/serviceImpl.java.vm");
        // 执行生成
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setTemplate(customizeTemplateConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent(packageName)
                                .setController("controller")
                                .setEntity("entity")
                                .setMapper("mapper")
                ).execute();
    }

    private static void generateByTables(String packageName, String... tableNames) {
        generateByTables(true, packageName, tableNames);
    }

}
